package com.yjyw.tiexie.common;

import android.app.Activity;
import android.app.Application;
import android.os.Bundle;

public class AppFrontBackHelper {
    private OnAppStatusListener mOnAppStatusListener;
    private long lastTime = 0;
    private Application.ActivityLifecycleCallbacks activityLifecycleCallbacks =
            new Application.ActivityLifecycleCallbacks() {
                private int activityStartCount = 0;

                @Override
                public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
                }

                @Override
                public void onActivityStarted(Activity activity) {
                    activityStartCount++;
                    if (activityStartCount == 1) {
                        if (mOnAppStatusListener != null) {
                            if (System.currentTimeMillis() - lastTime > 500) {
                                mOnAppStatusListener.onFront();
                            }
                            lastTime = System.currentTimeMillis();
                        }
                    }
                }

                @Override
                public void onActivityResumed(Activity activity) {
                }

                @Override
                public void onActivityPaused(Activity activity) {
                }

                @Override
                public void onActivityStopped(Activity activity) {
                    activityStartCount--;
                    if (activityStartCount == 0) {
                        if (mOnAppStatusListener != null) {
                            mOnAppStatusListener.onBack();
                        }
                    }
                }

                @Override
                public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
                }

                @Override
                public void onActivityDestroyed(Activity activity) {
                }
            };

    public AppFrontBackHelper() {
    }

    public void register(Application application, OnAppStatusListener listener) {
        mOnAppStatusListener = listener;
        application.registerActivityLifecycleCallbacks(activityLifecycleCallbacks);
    }

    public void unRegister(Application application) {
        application.unregisterActivityLifecycleCallbacks(activityLifecycleCallbacks);
    }

    public interface OnAppStatusListener {
        void onFront();

        void onBack();
    }
}
